package websocket.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import websocket.entity.DataLog;
import websocket.entity.vo.DataLogVo;
import websocket.service.DataLogService;

import java.math.BigDecimal;

public class JwdCalculationUtil {
    private static final Logger log = LoggerFactory.getLogger(JwdCalculationUtil.class);

    public double[] DistanceCalculation(double Lat, double Lon, double Hgt, double X, double Y, double Z) {
        double a = 6378137.0;
        double b = 6356752.3142;
        double e = Math.sqrt(((a * a) - (b * b)) / (a * a));

        double B = Lat;
        double L = Lon;
        double H = Hgt;

        //角度换成弧度
        B = B * Math.PI / 180;
        L = L * Math.PI / 180;

        X = (a / Math.sqrt(1 - (e * e) * (Math.sin(B) * Math.sin(B))) + H) * Math.cos(B) * Math.cos(L);
        Y = (a / Math.sqrt(1 - (e * e) * (Math.sin(B) * Math.sin(B))) + H) * Math.cos(B) * Math.sin(L);
        Z = (a / Math.sqrt(1 - (e * e) * (Math.sin(B) * Math.sin(B))) * (1 - (e * e)) + H) * Math.sin(B);
//        System.err.println(X + "~~~" + Y + "~~~" + Z);
        double[] data = new double[3];
        data[0] = X;
        data[1] = Y;
        data[2] = Z;
        return data;
    }

    /**
     * 根据两点的经纬高计算 空间距离
     * @param m_P1lon 经度1
     * @param m_P1lat 维度1
     * @param m_P1hgt 高度1
     * @param m_P2lon 经度2
     * @param m_P2lat 纬度2
     * @param m_P2hgt 高度2
     * @return 结果
     */
    public DataLogVo jisuan(double m_P1lon, double m_P1lat, double m_P1hgt, double m_P2lon, double m_P2lat, double m_P2hgt) {
        BigDecimal zero = new BigDecimal(0);
        double P1lat = 0;
        double P1lon = 0;
        double P1hgt = 0;
        double P2lat = 0;
        double P2lon = 0;
        double P2hgt = 0;
        double Orient = 0;
        double Distance = 0;
        double X = 0;
        double Y = 0;
        double Z = 0;
        double X1 = 0;
        double Y1 = 0;
        double Z1 = 0;
        double X2 = 0;
        double Y2 = 0;
        double Z2 = 0;
        P1lat = m_P1lat;
        P1lon = m_P1lon;
        P1hgt = m_P1hgt;
        P2lat = m_P2lat;
        P2lon = m_P2lon;
        P2hgt = m_P2hgt;

        if (P1lat < -90 || P1lat > 90) {
            log.info("请正确输入起点纬度");
            return null;
        } else if (P1lon < -180 || P1lon > 180) {
            log.info("请正确输入起点经度");
            return null;
        } else if (P2lat < -90 || P2lat > 90) {
            log.info("请正确输入终点纬度");
            return null;
        } else if (P2lon < -180 || P2lon > 180) {
            log.info("请正确输入终点经度");
            return null;
        }
        double[] data = this.DistanceCalculation(P1lat, P1lon, P1hgt, X, Y, Z);
        X1 = data[0];
        Y1 = data[1];
        Z1 = data[2];
//        System.err.println(X1 + "---" + Y1 + "---" + Z1);
        double[] data2 = this.DistanceCalculation(P2lat, P2lon, P2hgt, X, Y, Z);
        X2 = data2[0];
        Y2 = data2[1];
        Z2 = data2[2];
//        System.err.println(X2 + "---" + Y2 + "---" + Z2);
        Distance = Math.sqrt((X1 - X2) * (X1 - X2) + (Y1 - Y2) * (Y1 - Y2) + (Z1 - Z2) * (Z1 - Z2));
//        System.err.println("Distance:" + Distance);
//        BigDecimal orientFormat = new BigDecimal(Orient);
//        BigDecimal orientDecimal = orientFormat.setScale(5, BigDecimal.ROUND_HALF_UP);
//        System.err.println("orientDecimal" + orientDecimal);

        BigDecimal distanceFormat = new BigDecimal(Distance);
        BigDecimal distanceDecimal = distanceFormat.setScale(4, BigDecimal.ROUND_HALF_UP);

        DataLog log=new DataLog();
        log.setWrjLat(P1lat);
        log.setWrjLon(P1lon);
        log.setWrjHgt(P1hgt);

        log.setSpyLat(P2lat);
        log.setSpyLon(P2lon);
        log.setSpyHgt(P2hgt);

        log.setXj(Distance);
        log.setCreatTime(websocket.util.DateUtil.getCurrentTimeMillisecond());
        DataLogVo vo=new DataLogVo();
        vo.setDataLog(log);
        vo.setDistanceDecimal(distanceDecimal);
        //log.info("===计算斜距结果："+distanceDecimal);
        return vo;
    }


    public static void main(String[] args) {
        // 石家庄经纬度、北京大兴区经纬度
//        BigDecimal jisuan = new JwdCalculationUtil().jisuan(114.502461, 38.045474, 300,
//                116.33, 39.73, 5);
//        double dist=JwdCalculationUtil.GetDistance(114.502461, 38.045474, 116.33, 39.73);
//        double dist2=JwdCalculationUtil.distance(114.502461, 38.045474, 116.33, 39.73);
//        System.out.println("1.两点相距：" + jisuan + " 米");
//        System.out.println("2.两点相距：" + dist + " 米");
//        System.out.println("3.两点相距：" + dist2 + " 米");
        System.out.println(websocket.util.DateUtil.getCurrentTimeMillisecond());

    }


    private static double EARTH_RADIUS = 6371000;//赤道半径(单位m)

    /**
     * 转化为弧度(rad)
     * */
    private static double rad(double d)
    {
        return d * Math.PI / 180.0;
    }
    /**
     * @param lon1 第一点的精度
     * @param lat1 第一点的纬度
     * @param lon2 第二点的精度
     * @param lat2 第二点的纬度
     * @return 返回的距离，单位m
     * */
    public static double GetDistance(double lon1,double lat1,double lon2, double lat2) {
        double radLat1 = rad(lat1);
        double radLat2 = rad(lat2);
        double a = radLat1 - radLat2;
        double b = rad(lon1) - rad(lon2);
        double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
        s = s * EARTH_RADIUS;
        s = Math.round(s * 10000) / 10000;
        return s;
    }
    private static double distance(double centerLon, double centerLat, double targetLon, double targetLat) {

        double jl_jd = 102834.74258026089786013677476285;// 每经度单位米;
        double jl_wd = 111712.69150641055729984301412873;// 每纬度单位米;
        double b = Math.abs((centerLat - targetLat) * jl_jd);
        double a = Math.abs((centerLon - targetLon) * jl_wd);
        return Math.sqrt((a * a + b * b));
    }
}
